const httpUrl = "http://www.erp.com/api/v1";
const host = "http://www.erp.com/";
const alyhost = 'https://feichuangoss.oss-cn-shanghai.aliyuncs.com/oss_storage/';
const request = (url, options) => {
  wx.showLoading({
    title: '请求中..',
	  mask:true
  });
  console.log('请求接口',url);
  console.log('请求参数',options);
  return new Promise((resolve, reject) => {
    wx.request({
      url: httpUrl + url,
      method: options.method,
      data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
      header: {
        'Content-Type': 'application/json; charset=UTF-8',
        'X-Requested-With': 'XMLHttpRequest',
        'Authorization': wx.getStorageSync('token')
		// ,'env':'dav'
      },
      success(request) {
        console.log(request);
        if (request.statusCode == 401) {
          wx.setStorageSync('token', "");
          wx.showModal({
            title: '提示',
            content: '你的身份认证信息已过期，请到个人中心登录授权认证',
            success(res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: '/pages/login/login',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          uni.hideLoading();
          return false;
        }
        if (request.statusCode != 200) {
          wx.showToast({
            title: '接口返回出错' + request.statusCode,
            icon: 'none'
          })
          // reject(error.data)
          return false;
        }
        if (request.data.status == true) {
          console.log('请求成功',request.data);
          resolve(request.data.data)
        } else {
          wx.showToast({
            title: request.data.data.message,
            icon: "none"
          })
          // reject(request.data);
          return false;
        }
        wx.hideLoading()
      },
      fail(error) {
        reject(error)
        wx.hideLoading()
      }
    })
  })
}

const get = (url, options = {}) => {

  return request(url, {
    method: 'GET',
    data: options
  })
}

const post = (url, options) => {
  return request(url, {
    method: 'POST',
    data: options
  })
}

const put = (url, options) => {
  return request(url, {
    method: 'PUT',
    data: options
  })
}

// 不能声明DELETE（关键字）
const remove = (url, options) => {
  return request(url, {
    method: 'DELETE',
    data: options
  })
}



module.exports = {
  get,
  post,
  put,
  remove,
  host,
  httpUrl,
  alyhost
}